package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.RandomPoolUser;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface RandomPoolUserMapper {
    @Delete({
        "delete from random_pool_user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into random_pool_user (id, uid, sex, ",
        "anchor_uid, chat_id, ",
        "target_id, status, ",
        "create_time, update_time, ",
        "expire_time)",
        "values (#{id,jdbcType=BIGINT}, #{uid,jdbcType=BIGINT}, #{sex,jdbcType=TINYINT}, ",
        "#{anchorUid,jdbcType=BIGINT}, #{chatId,jdbcType=BIGINT}, ",
        "#{targetId,jdbcType=BIGINT}, #{status,jdbcType=TINYINT}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{expireTime,jdbcType=TIMESTAMP})"
    })
    int insert(RandomPoolUser record);

    int insertSelective(RandomPoolUser record);

    @Select({
        "select",
        "id, uid, sex, anchor_uid, chat_id, target_id, status, create_time, update_time, ",
        "expire_time",
        "from random_pool_user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    RandomPoolUser selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(RandomPoolUser record);

    @Update({
        "update random_pool_user",
        "set uid = #{uid,jdbcType=BIGINT},",
          "sex = #{sex,jdbcType=TINYINT},",
          "anchor_uid = #{anchorUid,jdbcType=BIGINT},",
          "chat_id = #{chatId,jdbcType=BIGINT},",
          "target_id = #{targetId,jdbcType=BIGINT},",
          "status = #{status,jdbcType=TINYINT},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "expire_time = #{expireTime,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(RandomPoolUser record);

    /***********以下为自定义方法************************/

    @Select({
            "select",
            "id, uid, sex, anchor_uid, chat_id, target_id, status, create_time, update_time, expire_time",
            "from random_pool_user",
            "where status = 1 and uid != #{selfUid,jdbcType=BIGINT} and expire_time > #{currTime,jdbcType=TIMESTAMP} for update"
    })
    @ResultMap("BaseResultMap")
    List<RandomPoolUser> getOnlineListForUpdate(@Param("selfUid") Long selfUid, @Param("currTime") Date currTime);

    @Select({
            "select",
            "id, uid, sex, anchor_uid, chat_id, target_id, status, create_time, update_time, expire_time",
            "from random_pool_user",
            "where status = 1 and sex = #{sex,jdbcType=TINYINT} and uid != #{selfUid,jdbcType=BIGINT} and expire_time > #{currTime,jdbcType=TIMESTAMP} for update"
    })
    @ResultMap("BaseResultMap")
    List<RandomPoolUser> getOnlineListBySexForUpdate(@Param("selfUid") Long selfUid, @Param("sex") Integer sex, @Param("currTime") Date currTime);

    @Select({
            "select",
            "id, uid, sex, anchor_uid, chat_id, target_id, status, create_time, update_time, expire_time",
            "from random_pool_user",
            "where uid = #{uid,jdbcType=BIGINT} and status = 1 and expire_time > #{currTime,jdbcType=TIMESTAMP} order by id desc limit 1 for update"
    })
    @ResultMap("BaseResultMap")
    RandomPoolUser selectRandomPoolUserByUid(@Param("uid") Long uid, @Param("currTime") Date currTime);

    @Update({
            "update random_pool_user",
            "set status = 3",
            "where status = 1 and uid = #{uid,jdbcType=BIGINT}"
    })
    int updateStatusOverByUid(Long uid);
}